package com.wiretap.action.admin.manage;

import java.io.PrintWriter;

import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.Message;
import com.wiretap.service.MessageService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.Pager;

public class MessageManageAction extends WiretapActionSupport {

	private static final long serialVersionUID = -5882916970662911379L;
	private MessageService messageService;
	private String phone;
	private String mid;
	private int cPage;
	private Pager<Message> message;

	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		if (!CommenUtil.isEmpty(phone)) {
			message = messageService.getMessageByOwner(phone, cPage,
					Constants.DEFAULT_PAGESIEZE);
		}
		return SUCCESS;
	}

	public void deleteMsg() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(mid)) {
			out.print("error");
			return;
		}
		String[] ids = mid.split(",");
		if (!messageService.deleteMessage(ids)){
			out.print("error");
			return;
		}
		out.print("success");
		out.flush();
		out.close();
	}
	
	public void getMsgCountByPhone() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(phone)) {
			out.print(0);
			return;
		}
		long count = messageService.getMessageCountByPhone(phone);
		out.print(count);
		out.flush();
		out.close();
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public Pager<Message> getMessage() {
		return message;
	}

	public void setMessage(Pager<Message> message) {
		this.message = message;
	}

}
